package elitech.vietnam.weatherfashion.ultilities;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;

public class GoogleMapService {

	public static String getCountryLanguageCode(final double lat,
			final double lon) {

		String code = "en";
		final String urlStr = "http://maps.googleapis.com/maps/api/geocode/json?latlng="
				+ lat + "," + lon + "&sensor=false";

		try {
			// Create a URL for the desired page
			URL url;
			url = new URL(urlStr);

			// Read all the text returned by the server
			BufferedReader in = new BufferedReader(new InputStreamReader(
					url.openStream()));

			String str = null;
			while ((str = in.readLine()) != null) {
				if (str.toLowerCase().contains("usa"))
					return "en";
				if (str.toLowerCase().contains("korea"))
					return "ko";
				if (str.toLowerCase().contains("china"))
					return "zh";
				if (str.toLowerCase().contains("vietnam"))
					return "vi";
			}
			in.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return code;
	}

	public static String getCountryCode(final double lat, final double lon) {

		final String urlStr = "http://maps.googleapis.com/maps/api/geocode/json?latlng="
				+ lat + "," + lon + "&sensor=false";

		try {
			// Create a URL for the desired page
			URL url;
			url = new URL(urlStr);

			// Read all the text returned by the server
			BufferedReader in = new BufferedReader(new InputStreamReader(
					url.openStream()));

			String str = null;
			while ((str = in.readLine()) != null) {
				if (str.toLowerCase().contains("usa"))
					return "US";
				if (str.toLowerCase().contains("korea"))
					return "KR";
				if (str.toLowerCase().contains("china"))
					return "CN";
				if (str.toLowerCase().contains("vietnam"))
					return "VN";
			}
			in.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return "";
	}

	
}
